package com.jzr.open.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jzr.common.entity.vo.Result;
import com.jzr.common.util.U;
import com.jzr.open.db1.model.TFee;
import com.jzr.open.db1.service.FeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/fee")
public class FeeController {
    @Autowired
    private FeeService feeService;

    @PostMapping("/get")
    public Result<List<TFee>> get(@RequestBody TFee record){
        if(U.isBlank(record.getPatientCode())){
            return Result.fail("patientCode不能为空");
        }
        QueryWrapper<TFee> qw = new QueryWrapper<>();
        qw.orderByAsc("patient_code","inv_no","item_code");

        qw.eq(TFee.PatientCode_,record.getPatientCode());

        if(U.isNotBlank(record.getInvNo())){
            qw.eq(TFee.InvNo_,record.getInvNo());
        }

        List<TFee> list = feeService.list(qw);
        return Result.success("成功",list,list==null ? 0:list.size());
    }

    @PostMapping("/post")
    public Result<Integer> post(@RequestBody List<TFee> list){
        if(list == null || list.isEmpty()){
            return Result.fail("数据不能为空");
        }
        for(TFee record : list){
            if(U.isBlank(record.getInvNo())){
                return Result.fail("invNo不能为空");
            }
            if(U.isBlank(record.getPatientCode())){
                return Result.fail("patientCode不能为空");
            }
            if(U.isBlank(record.getCureNo())){
                return Result.fail("cureNo不能为空");
            }
            if(U.isBlank(record.getItemCode())){
                return Result.fail("itemCode不能为空");
            }
        }
        int count = feeService.save(list);
        if(count <= 0){
            return Result.fail("保存失败");
        }else {
            return Result.success("成功",count,count);
        }
    }
//    @PostMapping("/delete")
//    public Result<Integer> delete(@RequestBody List<TFee> list){
//        if(list == null || list.isEmpty()){
//            return Result.fail("数据不能为空");
//        }
//        for(TFee record : list){
//            if(U.isBlank(record.getInvNo())){
//                return Result.fail("invNo不能为空");
//            }
//            if(U.isBlank(record.getItemCode())){
//                return Result.fail("itemCode不能为空");
//            }
//        }
//        int count = feeService.delete(list);
//        if(count <= 0){
//            return Result.fail("删除失败");
//        }else {
//            return Result.success("成功",count);
//        }
//    }
}
